package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Question;
import com.itheima.pojo.Role;
import com.itheima.service.QuestionManageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("questionManage")
public class QuestionManageController {


    @Reference(version = "1.0.0")
    private QuestionManageService questionManageService;


    /**
     * 查询全部题目信息
     * @return  全部题目
     */
    @RequestMapping("findAll")
    public Result findAll(){
        List<Question> list = questionManageService.findAll();
        return new Result(true,"查询所有题目成功！",list);
    }


    /**
     * 新增题目
     * @param question  题目实例
     * @return  统一返回结果
     */
    @RequestMapping("add")
    public Result add(@RequestBody Question question){
        //调用业务层添加方法
        questionManageService.add(question);
        //返回操作结果
        return new Result(true, "新增题目成功！");
    }


    /**
     * 编辑题目
     * @param question  题目实例
     * @return  统一返回结果
     */
    @RequestMapping("update")
    public Result update(@RequestBody Question question){
        //调用业务层修改方法
        questionManageService.update(question);
        //返回操作结果
        return new Result(true, "修改成功！");
    }

    /**
     * 根据ID删除题目
     * @param id    题目ID
     * @return  返回结果
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        questionManageService.delete(id);
        //返回操作结果
        return new Result(true, "题目删除成功！");
    }

    /**
     * 根据ID查询题目信息
     * @param id    题目ID
     * @return  返回结果
     */
    @RequestMapping("findById/{id}")
    public Result findById(@PathVariable int id){
        Question question = questionManageService.findById(id);
        return new Result(true,"查询题目信息成功！",question);
    }

    /**
     * 分页查询题目数据
     * @param queryPageBean 分页查询语句
     * @return  返回结果
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = questionManageService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
}